package com.example.wujinapi.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wujinapi.comm.R;
import com.example.wujinapi.comm.jwtUtils;
import com.example.wujinapi.entity.Address;
import com.example.wujinapi.entity.wxDecryptPhoneUser;
import com.example.wujinapi.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Address)表控制层
 *
 * @author makejava
 * @since 2023-07-10 22:37:42
 */
@RestController
@RequestMapping("address/")
public class AddressController{
    /**
     * 服务对象
     */
    @Resource
    private AddressService addressService;


    /**
     * 添加用户地址
     */
    @PostMapping("/addUserAdress")
    public R addUserAdress(@RequestBody Address address) throws Exception {
        if(address.getToken()==null){
            return R.error(400,"无效的凭证");
        }
        try {
            String token=address.getToken();
            String openid=jwtUtils.getToken(token).getClaim("openid").toString().replace("\"","");

            //读取最新用户地址数据返回
            if(!StringUtils.isEmpty(openid)){
                //添加收货地址
                address.setOpenid(openid);
                addressService.addAddress(address);
                List<Address> userAdrList=addressService.getAddressesByOpenid(openid);
                return R.ok().put("userAdrList",userAdrList);
            }else{
                return R.error(400,"用户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(400,"失败");
        }
    }

    /**
     * 更新收货地址
     */
    @PostMapping("/updateAdress")
    public R updateAdress(@RequestBody Address address) throws Exception {
        try {
            addressService.updateAddressById(address);
            return R.ok("地址更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(400,"地址更新失败");
        }
    }


    /**
     * 删除收货地址
     */
    @PostMapping("/deleteAdress")
    public R deleteAdress(@RequestBody Address address) throws Exception {
        try {
            addressService.deleteAddress(address.getId());
            return R.ok("删除收货地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(400,"删除收货地址失败");
        }
    }



    /**
     * 获取用户收货地址
     */
    @PostMapping("/getUserAdress")
    public R getUserAdress(@RequestBody Address address) throws Exception {
        if(address.getToken()==null){
            return R.error(400,"无效的凭证");
        }
        String token=address.getToken();
        String openid=jwtUtils.getToken(token).getClaim("openid").toString().replace("\"","");
        if(!StringUtils.isEmpty(openid)){
            List<Address> userAdrList=addressService.getAddressesByOpenid(openid);
            return R.ok().put("userAdrList",userAdrList);
        }else{
           return R.error(400,"用户不存在");
        }
    }




}

